﻿using AutoStar.Views;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace AutoStar.ViewModel
{
    public class WindowSerialVM : BaseViewModel
    {
        public WindowSerialVM()
        {
            Load();
            OpenSerial = new RelayCommand(OpenSerialPort);
            SendCommand = new RelayCommand(BtnSend_Click);
        }
        //public static MainWindowVM mainWindowVM = Global.MainVM;
        public void OpenSerialPort()
        {
            if (Global.printer.serialPortController.serialPort.IsOpen == false && !string.IsNullOrEmpty(SelectedSerialPorts))  //..此时如果串口未开启，则打开串口
            {
                Global.printer.serialPortController.serialPort.PortName = SelectedSerialPorts;
                Global.printer.serialPortController.serialPort.BaudRate = int.Parse(SelectedBaudRates);
                Global.printer.serialPortController.serialPort.Parity = (Parity)ParityBitInt;
                //Global.printer.serialPortController.serialPort.Parity = (Parity)int.Parse(SelectedParityBits);
                Global.printer.serialPortController.serialPort.DataBits = int.Parse(SelectedDataBits);
                Global.printer.serialPortController.serialPort.StopBits = (StopBits)int.Parse(SelectedStopBits);
                try
                {
                    Global.MainVM.SwitchSerial();
                    //Global.printer.XYToZero();
                }
                catch (Exception ex)
                {
                    Global.LogHelper.WriteLog(ex.Message);
                    MessageBox.Show("串口打开失败，请检查");
                    return;
                }
            }
            else//..如果此时串口已开启，则关闭串口
            {
                try
                {
                    Global.MainVM.SwitchSerial();
                }
                catch (Exception ex)
                {
                }
            }
            SerialButtonText = Global.printer.serialPortController.serialPort.IsOpen ? "关闭串口" : "打开串口";
            SerialEnabled = !Global.printer.serialPortController.serialPort.IsOpen;
        }
        public void Load()
        {
            SerialButtonText = "打开串口";
            SerialEnabled = true;
            SerialPorts = new ObservableCollection<string>();
            BaudRates = new ObservableCollection<string>();
            DataBits = new ObservableCollection<string>();
            StopBits = new ObservableCollection<string>();
            ParityBits = new ObservableCollection<string>();

            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                SerialPorts.Add(port);
            }
            if (SerialPorts.Count > 0)
                SelectedSerialPorts = SerialPorts[0];
            else
                SelectedSerialPorts = "";

            BaudRates.Add("19200");
            BaudRates.Add("38400");
            BaudRates.Add("43000");
            BaudRates.Add("57600");
            BaudRates.Add("76800");
            BaudRates.Add("115200");
            BaudRates.Add("128000");
            BaudRates.Add("230400");
            BaudRates.Add("256000");
            if (BaudRates.Count > 0)
                SelectedBaudRates = BaudRates[5];
            else
                SelectedBaudRates = "";

            DataBits.Add("5");
            DataBits.Add("6");
            DataBits.Add("7");
            DataBits.Add("8");
            if (DataBits.Count > 0)
                SelectedDataBits = DataBits[3];
            else
                SelectedDataBits = "";

            StopBits.Add("1");
            StopBits.Add("1.5");
            StopBits.Add("2");
            if (StopBits.Count > 0)
                SelectedStopBits = StopBits[0];
            else
                SelectedStopBits = "";

            ParityBits.Add("无");
            ParityBits.Add("奇校验");
            ParityBits.Add("偶校验");
            if (ParityBits.Count > 0)
                SelectedParityBits = ParityBits[0];
            else
                SelectedParityBits = "";
            if (SelectedParityBits == "无")
                ParityBitInt = 0;
            else if (SelectedParityBits == "奇校验")
                ParityBitInt = 1;
            else if (SelectedParityBits == "偶校验")
                ParityBitInt = 2;
            else
                ParityBitInt = 0;
        }
        public void BtnSend_Click()
        {
            int i = Global.printer.serialPortController.sendGCode(InputTextBox);
            if (i == 0)
                MessageBox.Show("发送成功！");
            else
                MessageBox.Show("发送失败！");
        }
        public void SendDataEvent(string str)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                //SendListBox.Add(str);
                SendListBox.Insert(0,str);
                SelectedSendListBox = str;
            }));
        }
        public void GetDataEvent(string str)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                ReceiveListBox.Insert(0,str);
                if (ReceiveListBox.Count() > 150)
                {
                    ReceiveListBox.RemoveAt(0);
                }
                SelectedReceiveListBox = str;
            }));
        }
        public void WindowsSerial_Closing()
        {
            if (Global.WindowSerial != null)
            {
                //Global.WindowSerial.Close();
                Global.WindowSerial.Visibility = Visibility.Hidden;
            }
        }

        #region 控件参数
        public ICommand SendCommand { get; private set; }
        public ICommand OpenSerial { get; private set; }
        private ObservableCollection<string> _serialPorts;
        public ObservableCollection<string> SerialPorts
        {
            get => _serialPorts;
            set => Set(ref _serialPorts, value);
        }

        private ObservableCollection<string> _baudRates;
        public ObservableCollection<string> BaudRates
        {
            get => _baudRates;
            set => Set(ref _baudRates, value);
        }

        private ObservableCollection<string> _dataBits;
        public ObservableCollection<string> DataBits
        {
            get => _dataBits;
            set => Set(ref _dataBits, value);
        }

        private ObservableCollection<string> _stopBits;
        public ObservableCollection<string> StopBits
        {
            get => _stopBits;
            set => Set(ref _stopBits, value);
        }

        private ObservableCollection<string> _parityBits;
        public ObservableCollection<string> ParityBits
        {
            get => _parityBits;
            set => Set(ref _parityBits, value);
        }

        private string _selectedSerialPorts;
        public string SelectedSerialPorts
        {
            get => _selectedSerialPorts;
            set => Set(ref _selectedSerialPorts, value);
        }

        private string _selectedBaudRates;
        public string SelectedBaudRates
        {
            get => _selectedBaudRates;
            set => Set(ref _selectedBaudRates, value);
        }

        private string _selectedDataBits;
        public string SelectedDataBits
        {
            get => _selectedDataBits;
            set => Set(ref _selectedDataBits, value);
        }

        private string _selectedStopBits;
        public string SelectedStopBits
        {
            get => _selectedStopBits;
            set => Set(ref _selectedStopBits, value);
        }

        private int _ParityBitInt;
        public int ParityBitInt
        {
            get => _ParityBitInt;
            set => Set(ref _ParityBitInt, value);
        }
        private string _selectedParityBits;
        public string SelectedParityBits
        {
            get => _selectedParityBits;
            set => Set(ref _selectedParityBits, value);
        }
        private string _serialButtonText;
        public string SerialButtonText
        {
            get => _serialButtonText;
            set => Set(ref _serialButtonText, value);
        }
        private bool _serialEnabled;
        public bool SerialEnabled
        {
            get => _serialEnabled;
            set => Set(ref _serialEnabled, value);
        }
        private ObservableCollection<string> _sendListBox = new ObservableCollection<string>();
        public ObservableCollection<string> SendListBox
        {
            get => _sendListBox;
            set => Set(ref _sendListBox, value);
        }
        private ObservableCollection<string> _receiveListBox = new ObservableCollection<string>();
        public ObservableCollection<string> ReceiveListBox
        {
            get => _receiveListBox;
            set => Set(ref _receiveListBox, value);
        }
        private string _sendText;
        public string SendText
        {
            get => _sendText;
            set => Set(ref _sendText, value);
        }
        private string _selectedReceiveListBox;
        public string SelectedReceiveListBox
        {
            get => _selectedReceiveListBox;
            set => Set(ref _selectedReceiveListBox, value);
        }
        private string _buttonText;
        public string ButtonText
        {
            get => _buttonText;
            set => Set(ref _buttonText, value);
        }
        private string _selectedSendListBox;
        public string SelectedSendListBox
        {
            get => _selectedSendListBox;
            set => Set(ref _selectedSendListBox, value);
        }
        private string _InputTextBox = "";
        public string InputTextBox
        {
            get => _InputTextBox;
            set => Set(ref _InputTextBox, value);
        }
        #endregion
    }
}
